package com.thy.controller;


import com.hospital.common.base.PageResult;
import com.hospital.common.base.Result;
import com.hospital.common.base.ThreadAttributes;
import com.hospital.common.dto.management.module.BackendDoctorInfoDto;
import com.hospital.common.dto.management.module.DoctorInfoDto;
import com.hospital.common.dto.management.module.DoctorQueryDto;
import com.hospital.common.dto.management.module.RegisterDto;
import com.hospital.common.pojo.TDoctorInfo;
import com.hospital.common.pojo.TUser;
import com.hospital.common.utils.ThreadLocalUtils;
import com.hospital.common.vo.management.module.DoctorInfoVo;
import com.thy.service.TDoctorInfoService;
import com.thy.service.TUserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.hospital.common.base.interfaces.Const.CONTENT_TYPE_JPG;
import static com.hospital.common.base.interfaces.Const.CONTENT_TYPE_PNG;

/**
 * <p>
 * 医生信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-12
 */
@RestController
@RequestMapping("/doctor/info")
public class TDoctorInfoController {
    @Autowired
    private TDoctorInfoService tDoctorInfoService;
    @Autowired
    private TUserService tUserService;

    /**
     * 获取所有医生信息
     * @author iT唐
     * @return List<DoctorInfoVo>
     */
    @GetMapping("/getAllDoctor")
    public Result<List<DoctorInfoVo>> getAllDoctor() {
        List<DoctorInfoVo> doctorInfoVos = tDoctorInfoService.getAllDoctor();
        return Result.success(doctorInfoVos);
    }

    /**
     * 按照限制个数获取医生信息
     * @param num 限制个数 |5
     */
    @GetMapping("/getDoctorLimit")
    public Result<List<DoctorInfoVo>> getDoctorLimit(@RequestParam("num") Integer num){
        return Result.success(tDoctorInfoService.getDoctorLimit(num));
    }
    /**
     * 根据科室和名字获取医生信息
     * @param departmentId,name
     * @author iT唐
     * @return PageResult<DoctorInfoVo>
     */
    @GetMapping("/getDoctor/byDepartmentAndName")
    public Result<PageResult<DoctorInfoVo>> getDoctorByDepartmentAndName(@RequestParam("departmentId") Long departmentId,@RequestParam("name")String name,@RequestParam("pageNum") Long pageNum,@RequestParam("pageSize")Long pageSize){
        PageResult<DoctorInfoVo> divs = tDoctorInfoService.getDoctorByDepartmentAndName(departmentId,name,pageNum,pageSize);
        return Result.success(divs);
    }

    /**
     * 医生个人信息
     * @author iT唐
     * @return DoctorInfoVo
     */
    @GetMapping("/getDoctor")
    public Result<DoctorInfoVo> getDoctor(){
        ThreadAttributes threadAttributes = ThreadLocalUtils.get();
        Long userId = threadAttributes.getUserId();
        DoctorInfoVo doctorInfoVo = tDoctorInfoService.getDoctor(userId);
        return Result.success(doctorInfoVo);
    }

    /**
     * 获取医生信息
     * @param userId 医生id
     */
    @GetMapping("/getDoctorById")
    public Result<DoctorInfoVo> getDoctor(@RequestParam("userId") Long userId){
        return Result.success(tDoctorInfoService.getDoctor(userId));
    }

    /**
     * 添加医生
     * @param doctorInfo
     * @param user
     * @author iT唐
     * @return String
     */
    @GetMapping("/addDoctor")
    public Result<String> addDoctor(@RequestBody TDoctorInfo doctorInfo, @RequestBody TUser  user){
        tDoctorInfoService.addDoctor(user,doctorInfo);
        return Result.success("添加成功");
    }

    /**
     * 修改医生详细信息
     * @param doctorInfo
     * @return String
     */
    @PostMapping("/updateDoctor")
    public Result<String> updateDoctor(@RequestBody TDoctorInfo doctorInfo){
        ThreadAttributes threadAttributes = ThreadLocalUtils.get();
        Long userId = threadAttributes.getUserId();
        tDoctorInfoService.updateDoctor(doctorInfo,userId);
        return Result.success("修改成功");
    }

    /**
     * 修改医生信息
     */
    @PostMapping("/update")
    public Result<?> updateDoctor(@RequestBody DoctorInfoDto doctorInfoDto){
        tUserService.updateUser(doctorInfoDto);
        return Result.success();
    }
    /**
     * 删除医生
     * @return String
     */
    @DeleteMapping("/deleteDoctor")
    public Result<String> deleteDoctor(@RequestParam("userId") Long userId){
        tDoctorInfoService.deleteDoctor(userId);
        return Result.success("删除成功");
    }

    /**
     * 上传图片
     * @param file
     * @return String
     * @throws Exception
     */
    @PostMapping("/update/img")
    public Result<String> updateImg(@RequestParam("file") MultipartFile file)throws Exception{
        if (!CONTENT_TYPE_PNG.contains(file.getContentType())&&!CONTENT_TYPE_JPG.contains(file.getContentType())) {
            throw new IllegalArgumentException("不支持的文件类型，仅支持: " + CONTENT_TYPE_PNG+"|"+CONTENT_TYPE_JPG);
        }
        String objectName = tDoctorInfoService.updateImg(file);
        return Result.success("上传成功",objectName);
    }

    /**
     * 获取总医生统计
     */
    @GetMapping("/getDoctorCount")
    public Result<Long> getDoctorCount(){
        return Result.success(tUserService.allDoctorCount());
    }

    /**
     * 分页获取所有医生信息
     * @param pageNum 页码 |1
     * @param pageSize 每页数量 |10
     */
    @GetMapping("/getDoctorInPage")
    public Result<PageResult<DoctorInfoVo>> getDoctorInPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageResult<DoctorInfoVo> doctorInfoVos = tDoctorInfoService.getDoctorInPage(pageNum, pageSize);
        return Result.success(doctorInfoVos);
    }
    /**
     * 获取科室下所有医生信息
     * @param departmentId 科室id
     */
    @GetMapping("/getDoctorByDepartment")
    public Result<List<DoctorInfoVo>> getDoctorByDepartment(@RequestParam("departmentId") Long departmentId) {
        return Result.success(tDoctorInfoService.getDoctorByDepartment(departmentId));
    }
    /**
     * 获取科室下所有医生信息
     */
    @PostMapping("/getDoctorInPageByDepartment")
    public Result<PageResult<DoctorInfoVo>> getDoctorInPageByDepartment(@Valid @RequestBody DoctorQueryDto doctorQueryDto) {
        PageResult<DoctorInfoVo> doctorInfoVos = tDoctorInfoService.getDoctorInPageByDepartment(doctorQueryDto);
        return Result.success(doctorInfoVos);
    }

    /**
     * 管理员修改医生信息
     */
    @PostMapping("/adminUpdateDoctorInfo")
    public Result<?> adminUpdateDoctorInfo(@Valid @RequestBody BackendDoctorInfoDto backendDoctorInfoDto){
        tDoctorInfoService.adminUpdateDoctorInfo(backendDoctorInfoDto);
        return Result.success();
    }

    /**
     * 注册医生
     */
    @PostMapping("/addDoctor")
    public Result<?> register(@Valid @RequestBody RegisterDto registerDto){
        tDoctorInfoService.register(registerDto);
        return Result.success();
    }
}
